import { login } from '@/api/login'
import { getUserInfoAPI, logoutAPI } from '@/api/user.js'
import { Message } from 'element-ui'
const state = {
  token: '',
  userInfo: {}
}
const mutations = {
  setToken (state, value) {
    state.token = value
  },
  setUserInfo (state, value) {
    state.userInfo = value
  },
  async logout (state) {
    // const result = await logoutAPI()
    // if (result.code === 200) {
    //   Message.success('退出成功')
    // }
    state.token = ''
    state.userInfo = ''
  }
}
const actions = {
  async updateToken (store, value) {
    const res = await login(value)
    console.log('返回值', res)
    // 判断返回来的状态码为200时才是登陆正确
    // if (res.code === 200) {
    store.commit('setToken', res.data.token)
    Message.success('登陆成功')
  },
  async getUserInfo (store) {
    const result = await getUserInfoAPI()
    store.commit('setUserInfo', result.data)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
